(function(){
	'use strict';

	/**
	 * ZHX CTC APP
	 *
	 * Author : Vicco Wang
	 * Date : 2016.10.10
	 *
	 * Dependecies
	 * [zhxCtcLineModule] 直线图组件
	 * [zhxCtcAlarmModule] 报警信息列表组件
	 * [zhxForecastGridModule] 预测信息列表组件
	 * 
	 */
	var app = angular.module('zhxCTC',['zhxCtcLineModule','zhxCtcAlarmModule','zhxForecastGridModule']);

	app.controller('zhxCTCContrl',['$scope','$http','$interval','$timeout',function($scope,$http,$interval,$timeout){

		/**
		 * THESE ARE CREATED FOR TESTING API & DATA
		 */
		var __newId = function(){
			var date,seed,char,randomChar="",string;
	        date = new Date().getTime();
	        seed = parseInt( Math.random(1,1000) * 1000 );
	        char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	        for( var i = 0 ; i < 5; i++ ){
	            randomChar += char.charAt(Math.floor(Math.random() * 52));
	        }
	        string = String(seed + date) + randomChar;
	        return 'bus-' + string.substr(5);
		};
		var __newBusNo = function(){
			var randomChar = "陕A", char = "1234567890";
	        for( var i = 0 ; i < 5; i++ ){
	        	randomChar += char.charAt(Math.floor(Math.random() * 10));
	        }
	        return randomChar;
		};
		var __getRandomNo = function(Max,Min){
			var Range = Max - Min;  
			var Rand = Math.random();  
			return (Min + Math.round(Rand * Range)); 
		};
		var __getTheNo = function(chars){
			var char = chars;
			return char.charAt(Math.floor(Math.random() * chars.length));
		};

		var __getRate = function(){
			 return Math.random().toString().substring(5,'.');
		};

		//动态新增BUS
		var __addNewBus = function(){
			var busObj = {
				"lineType": __getTheNo("01"),
			    "busUuid": __newId(),
			    "busPlate": __newBusNo(),
			    "status"	: __getTheNo("012389"),
			    "planTime": "2016-10-11 16:05",
			    "rate"	: __getRate()
			};

			return busObj;
		};

		//模拟车辆在直线图上动态运行
		var TEMPBUS = []
		var __renderAnimateBus = function(){

			$interval(function(){

				var randomNum = __getRandomNo(0,TEMPBUS.length - 1);

				var randomRate = __getRandomNo(0.001,0.01);

				var bus = TEMPBUS[randomNum];

				if( bus.status == "0" ){

					var percent = parseFloat( bus.rate );

					if( percent <= 1 ){
						percent += randomRate;
						var busObj = {
							"lineType": bus.lineType,
						    "busUuid": bus.busUuid,
						    "busPlate": bus.busPlate,
						    "status"	: bus.status,
						    "planTime": bus.planTime,
						    "rate"	: bus.rate = percent.toFixed(3)
						};
					}
					$scope.ctcOptions.busData = busObj;
				}
				
			}, 2000 )

		}


		/**
		 * 直线图配置项
		 * @type {Object}
		 */
		$scope.ctcOptions = {
			//传递一个当前控制器的作用域，方便后面组件中使用
			scope : $scope,
			//线路数据
			// lineData : null,
			//线路数据字段映射
			// lineKeyMap : {},
			//车辆数据，每次推送1辆车数据，根据ID判断为新增车辆还是更新车辆[需要实时更新]
			// busData : null,
			//车辆数据字段映射
			// busKeyMap : {},
			//线路配置项 左上角车辆，上下行展示项
			// lineOptsData : null,
			lineOptions : [
				{ "displayName" : "车辆", "key" : "totalBus" },
				{ "displayName" : "配车", "key" : "peiche" },
				{ "displayName" : "上行", "key" : "upStation" },
				{ "displayName" : "下行", "key" : "downStation" },
				{ "displayName" : "司机", "key" : "drivers" },
				{ "displayName" : "加长型标题", "key" : "others" },
				{ "displayName" : "文字", "key" : "words" },
			],
			//控制区域按钮配置项，
			ctrlButtons : [
				{ "displayName": "实时排班", 		"event" : "clickToEvent1" },
				{ "displayName": "广播消息群发",		"event" : "clickToEvent2" },
				{ "displayName": "报警信息", 		"event" : "clickToEvent3" },
				{ "displayName": "临时排班", 		"event" : "clickToEvent4" }
			],
			//线路支线标签页数据配置
			// lineTab : null,
			lineTabOpts : {
				"id" : "lineId",
				"name" : "lineName",
				"event" : "changeSubLine"	//切换子线路事件定义
			},
			//车辆点击后环状图标组按钮样式配置与事件配置
			//event事件可以直接在该控制器内定义
			//event事件接收两个参数，event对象和event按钮的scope对象
			busClickOpts : [
				{ "name": "1", "icon": "iconfont icon-dianhua",		"color": "green",		"event" : "event_1" },
				{ "name": "2", "icon": "iconfont icon-tongzhi", 	"color": "purple",		"event" : "event_2" },
				{ "name": "3", "icon": "iconfont icon-shandian", 	"color": "darkOrange",	"event" : "event_3" },
				{ "name": "4", "icon": "iconfont icon-icon05", 		"color": "darkBlue",	"event" : "event_4" },
				{ "name": "5", "icon": "iconfont icon-shipin", 		"color": "orange",		"event" : "event_5" },
				{ "name": "6", "icon": "iconfont icon-huhuan", 		"color": "lightBlue",	"event" : "event_6" },
				{ "name": "7", "icon": "iconfont icon-rili", 		"color": "flyBlue",	"event" : "event_7" },
				{ "name": "8", "icon": "iconfont icon-gengduo", 	"color": "gray",	"event" : "event_8" }
			]

		};

		/**
		 * 预测信息GRID配置项
		 */
		$scope.gridOptions = {
			scope : $scope,
			data : null,
			columns : [
				{ "name" : "bus_number", 		"displayName" : "车牌号",		"width": 80  },
				{ "name" : "drv_name",			"displayName" : "司机",			"width": 80  },
				{ "name" : "shift",				"displayName" : "班次",			"width": 40  },
				{ "name" : "trip",				"displayName" : "趟次",			"width": 40  },
				{ "name" : "updown",			"displayName" : "上下行",		"width": 60  },
				{ "name" : "planTime",			"displayName" : "计划发车",		"width": 80,	"filter" : "busTimeFilter" },
				{ "name" : "realTime",			"displayName" : "实际发车",		"width": 80,	"filter" : "busTimeFilter"   },
				{ "name" : "nowStation",		"displayName" : "当前站",		"width": 100  },
				{ "name" : "toFinalStationNum",	"displayName" : "距终点站数",		"width": 100  },
				{ "name" : "toFinalStationTime","displayName" : "距终点时间(分)",		"width": 100  },
				{ "name" : "toFinalStationDate","displayName" : "预计到达终点时刻",		"width": 110,	"filter" : "busTimeFilter" },
				{ "name" : "nextTripTime",		"displayName" : "下一趟发车时间",		"width": 110,	"filter" : "busTimeFilter" },
				{ "name" : "forecastNote",		"displayName" : "预测提醒", "width" : 200 }
			],
			event : function(row,rowData, event){
				//处理过后通过该方法移除行
				// row.processed();
			}
		};

		/**
		 * 报警信息配置项
		 */
		$scope.alarmOptions = {
			scope : $scope,
			data : null,
			//该字段标识报警信息的重要程度
			levelKey : 'type_code',
			//HIGH 红色  MIDDEL 黄色  LOW 绿色
			level : { "high" : 40, "middle" : 32, "low" : 30 },
			columns : [
				{ "name" : "bus_number", 	"displayName" : "车牌号",	"width": 80  },
				{ "name" : "start_time",	"displayName" : "申报时间",	"width": 80, "filter" : "busTimeFilter" },
				{ "name" : "type_name",		"displayName" : "申报类型",	"width": 100  }
			],
			event : function(row,rowData, event){
				//处理过后通过该方法移除行
				row.processed();
			}
		};

		//模拟线路信息
		$http.get('data/line-data.json').then(function(res){
				//LOAD LINE DATA
				$scope.ctcOptions.lineData = res.data.data;

				//CREATE AUTOMIC DATA FOR TESTING
				//模拟车辆信息
				var times = 0;
				var getBusPerSec = $interval(function(){
					if( times == 50 ) {
						times = 0;
						$interval.cancel(getBusPerSec);
					}
					$scope.ctcOptions.busData = __addNewBus();

					TEMPBUS.push( $scope.ctcOptions.busData );

					times++;

				},30);

				//开启模拟车辆动态刷新
				__renderAnimateBus();

		});

		$http.get('data/lines.json').then(function(res){
			$scope.ctcOptions.lineTab = res.data;
		})
		//模拟当前线路 车辆 上下行 配车数等信息获取
		$http.get('data/lineStationOpts.json').then(function(res){
			$scope.ctcOptions.lineOptsData = res.data;
		});
		//模拟预测信息
		$http.get('data/grid.json').then(function(res){
			$scope.gridOptions.data = res.data;
		});
		//模拟报警信息
		$http.get('data/alarm.json').then(function(res){
			$scope.alarmOptions.data = res.data;
		});


		// $scope.clickToEvent1 = function(){
		// 	console.log('ctrl buttons1 click.');
		// }

		//车辆环状按钮事件定义
		// $scope.event_1 = function(event, busScope, evScope){
		// 	console.log('bus click event1');
		// 	console.log( busScope );
		// }
		// 
		
		//切换子线路按钮 大站快车，支线，区间TAB按钮
		//接收切换后的LINE对象
		$scope.changeSubLine = function(line){
			console.log(line)
		}


	}]);


})()


